﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DTO;
using DataService;

public partial class Admin_AccountUser : BasePage
{
    private UserService _userService;
    private IList<User> _users;
    public Admin_AccountUser()
    {
        _userService = new UserService();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (AdminLogin == null)
            Response.Redirect("~/admin/login.aspx");

        if (!string.IsNullOrEmpty(Request.QueryString["ac"]))
        {
            if (!IsPostBack)
            {
                LoadGrid();
                lbAccount.Text = "User for Customer <b>" + Convert.ToString(Request.QueryString["name"]) + "</b>";
            }
        }
        else
        {
            Response.Write("No User Account ....");
        }
    }

    private void LoadGrid()
    {
        _users = _userService.GetListUserByAccountId(Convert.ToInt32(Request.QueryString["ac"]));           
        gvUsers.DataSource = _users;
        gvUsers.DataBind();
    }
    protected void gvUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvUsers.EditIndex = e.NewEditIndex;
        LoadGrid();
    }
    protected void gvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int UserId = Convert.ToInt32(gvUsers.Rows[e.RowIndex].Cells[0].Text);
        _userService.DeleteUserByUserId(UserId);
        LoadGrid();
    }
    protected void gvUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int UserId = Convert.ToInt32(gvUsers.Rows[e.RowIndex].Cells[0].Text);
        TextBox txtPassword = (TextBox)gvUsers.Rows[e.RowIndex].Cells[2].Controls[0];
        TextBox txtFirstName = (TextBox)gvUsers.Rows[e.RowIndex].Cells[3].Controls[0];
        TextBox txtLastName = (TextBox)gvUsers.Rows[e.RowIndex].Cells[4].Controls[0];
        CheckBox chkIsActive = (CheckBox)gvUsers.Rows[e.RowIndex].Cells[5].Controls[0];
        CheckBox chkIsAdmin = (CheckBox)gvUsers.Rows[e.RowIndex].Cells[6].Controls[0];

        _userService.UpdateUserInfo(UserId, txtPassword.Text, txtFirstName.Text, txtLastName.Text, chkIsActive.Checked, chkIsAdmin.Checked);
        gvUsers.EditIndex = -1;
        LoadGrid();
    }
    protected void gvUsers_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvUsers.EditIndex = -1;
        LoadGrid();
    }
}